//
//  zhanghumingxiVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/4/7.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "zhanghumingxiVC.h"
#import "Masonry.h"

#import "Controller.h"

#import "SVPullToRefresh.h"

#import "labelview.h"
#import "TongYong.h"

#import "chongzhiVC.h"
#import "NSString+formatWithFloat.h"

#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1]


@interface zhanghumingxiVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *bigview;
    UIView *topsmallview;
    
    NSMutableArray *arrayList;//报价数组；
    UILabel *mmoneyla;//账户余额
    
    labelview *la;//无数据显示的页面
}
@property(nonatomic,strong) UITableView *tableview;  //定义大表格
@property (nonatomic, assign)NSInteger  page;//页数
@end

@implementation zhanghumingxiVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"zhanghumingxiVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"zhanghumingxiVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(60);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"资金账户";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.top.offset(31);
    }];

    
    //编辑按钮
    UIButton *rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"充值" forState:UIControlStateNormal];
    [rightButton setSelected:NO];
    [rightButton addTarget:self action:@selector(chongzhibut) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:rightButton];
    //    rightButton.frame=CGRectMake(screenWidth-6-80, 60-6-28, 80, 28);
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-6);
        make.width.offset(80);
        make.height.offset(28);
        make.bottom.offset(-6);
    }];

    bigview=[[UIView alloc]initWithFrame:CGRectMake(0, 60, screenWidth, screenHeight-96+36)];
    bigview.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    [self.view addSubview:bigview];
    
    
    UIView *headerview=[[UIView alloc]initWithFrame:CGRectMake(0, 5, screenWidth, 100-30)];
    headerview.backgroundColor=[UIColor whiteColor];
    [bigview addSubview:headerview];
    
    UILabel *yuela=[[UILabel alloc]init];
    yuela.frame=CGRectMake((self.view.frame.size.width-100)/2, 10, 100, 20);
    yuela.font=[UIFont systemFontOfSize:12];
    yuela.textAlignment=NSTextAlignmentCenter;
    yuela.textColor=[UIColor blackColor];
    yuela.text=@"账户余额";
    yuela.backgroundColor=[UIColor clearColor];
    [headerview addSubview:yuela];

    
    mmoneyla=[[UILabel alloc]init];
    mmoneyla.frame=CGRectMake((self.view.frame.size.width-300)/2, 30, 300, 30);
    mmoneyla.font=[UIFont systemFontOfSize:16];
    mmoneyla.textAlignment=NSTextAlignmentCenter;
    mmoneyla.textColor=[UIColor redColor];
//    mmoneyla.text=@"￥45555.00";
    mmoneyla.backgroundColor=[UIColor clearColor];
    [headerview addSubview:mmoneyla];

    
    UIView *conterView=[[UIView alloc]init];
    conterView.backgroundColor=[UIColor clearColor];
    [bigview addSubview:conterView];
    [conterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.width.offset(screenWidth);
        make.bottom.offset(0);
        make.top.offset(110-30);
    }];

    
    UIView *top=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 70)];
    top.backgroundColor=[UIColor whiteColor];
    [conterView addSubview:top];

    

    UILabel *mingxi=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 40)];
    mingxi.text=@"收支明细";
    mingxi.font=[UIFont systemFontOfSize:12];
    mingxi.textAlignment=NSTextAlignmentCenter;
    mingxi.textColor=[UIColor redColor];
    mingxi.backgroundColor=[UIColor whiteColor];
    [top addSubview:mingxi];
    
    UIView *fenge1=[[UIView alloc]initWithFrame:CGRectMake(0, 39.5, screenWidth, 0.5)];
    fenge1.backgroundColor=[UIColor grayColor];
    [top addSubview:fenge1];
    
    
    NSArray *ordername=@[@"发生时间",@"类型",@"收入",@"支出"];
   
    for (NSInteger i=0; i<4; i++) {
        //定义名称
        NSInteger kuan=(self.view.frame.size.width-20)/4;
        UILabel *namela=[[UILabel alloc]init];
        namela.text=ordername[i];
        namela.font=[UIFont systemFontOfSize:12];
        namela.textColor=[UIColor grayColor];
        namela.backgroundColor=[UIColor whiteColor];
        namela.frame=CGRectMake(10+kuan*i, 40, kuan, 30);
        namela.textAlignment=NSTextAlignmentLeft;
        [top addSubview:namela];
        
    }
    
    UIView *fenge2=[[UIView alloc]initWithFrame:CGRectMake(0, 69.5, screenWidth, 0.5)];
    fenge2.backgroundColor=[UIColor grayColor];
    [top addSubview:fenge2];

    
    arrayList=[[NSMutableArray alloc]init];
    
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,70, self.view.frame.size.width, screenHeight-180+36) style:UITableViewStylePlain];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    [conterView addSubview:_tableview];

    //放置下拉上拉刷新控件
    __weak zhanghumingxiVC *weakSelf = self;
    
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
    // setup infinite scrolling
    [self.tableview addInfiniteScrollingWithActionHandler:^{
        [weakSelf insertRowAtBottom];
    }];
    
    //刷新数据
    [self  insertRowAtTop];
    
    
}

- (void)insertRowAtTop {
    __weak zhanghumingxiVC *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    _page=0;
    NSNumber*number1=[NSNumber numberWithInteger:0];
    NSNumber*number2=[NSNumber numberWithInteger:20];
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    [di setValue:number1 forKey:@"page_num"];
    [di setValue:number2 forKey:@"page_step"];
    
    [Controller requestWithURL:QueryAcountLog jsondic:di finishBlock:^(ClassData* data) {
        
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            NSString *yue=[NSString stringWithFormat:@"%@",data.data[@"available_money"]];
            
            NSDecimalNumber *available_money=[[NSDecimalNumber alloc]initWithString:yue];
            
            mmoneyla.text=[NSString stringWithFormat:@"￥%@",[NSString notRounding:available_money afterPoint:2]];
            
            [arrayList removeAllObjects];
            
            [arrayList addObjectsFromArray:data.data[@"data"]];

            //判断 是否  有 无数据
            if (arrayList.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }
                la=[[labelview alloc]init];
                [la setimage:@"dingdanNew" tit:@"暂无收支数据" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.image.frame=CGRectMake((self.view.frame.size.width-75)/2, 50, 75, 100);
                la.label.frame=CGRectMake(50, 150, self.view.frame.size.width-100, 30);
                [_tableview addSubview:la];
                
                
            }else{
                
                if (la) {
                    [la removeFromSuperview];
                }
                [self.tableview reloadData];
                
                
            }

            
        }
        else
        {
        }
    }];
    
    // 3. 结束刷新(相当于动画)
    [weakSelf.tableview endUpdates];
    
    [weakSelf.tableview.pullToRefreshView stopAnimating];
    
    
    
}


- (void)insertRowAtBottom {
    
    __weak zhanghumingxiVC *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    _page=_page+1;
    NSNumber*number1=[NSNumber numberWithInteger:_page];
    NSNumber*number2=[NSNumber numberWithInteger:20];
    
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    [di setValue:number1 forKey:@"page_num"];
    [di setValue:number2 forKey:@"page_step"];
    
    [Controller requestWithURL:QueryAcountLog jsondic:di finishBlock:^(ClassData* data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            [arrayList addObjectsFromArray:data.data[@"data"]];
            
            [self.tableview reloadData];

            
        }
        else
        {
            
        }
    }];
    
    [weakSelf.tableview endUpdates];
    [weakSelf.tableview.infiniteScrollingView stopAnimating];
    
}

//充值按钮
-(void)chongzhibut
{
    chongzhiVC*czVC=[[chongzhiVC alloc]init];
    czVC.ismingxi=@"账户明细";
    [self.navigationController pushViewController:czVC animated:YES];

    
}
//提现
-(void)tixianbut
{

    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return arrayList.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //指定cellIdentifier为自定义的cell
    static NSString *CellIdentifier = @"Cell";
    
    //自定义cell类有图片
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor whiteColor];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
//        NSArray *namelaarray=@[@"2014",@"支付",@"500",@"6000"];
        
        for (NSInteger i=0; i<4; i++) {
            //定义名称
            NSInteger kuan=(self.view.frame.size.width-20)/4;
            UILabel *namela=[[UILabel alloc]init];
//            namela.text=namelaarray[i];
            namela.tag=1000+i;
            namela.font=[UIFont systemFontOfSize:12];
            namela.textColor=[UIColor grayColor];
            namela.backgroundColor=[UIColor whiteColor];
            namela.frame=CGRectMake(10+kuan*i,0, kuan, 30);
            namela.textAlignment=NSTextAlignmentLeft;
            [cell addSubview:namela];
        
        }
        
        UIView *fenge1=[[UIView alloc]initWithFrame:CGRectMake(0, 29.5, screenWidth, 0.5)];
        fenge1.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
        [cell addSubview:fenge1];
        
        
    }
    
    NSMutableArray *cellarray=[[NSMutableArray alloc]init];
    
    NSString *timest=[NSString stringWithFormat:@"%@",arrayList[indexPath.row][@"create_time"]];
    
    NSString *typest=[NSString stringWithFormat:@"%@",arrayList[indexPath.row][@"type"]];
    
    NSString *in_com=[NSString stringWithFormat:@"%@",arrayList[indexPath.row][@"in_coming"]];
    
    NSString *out_com=[NSString stringWithFormat:@"%@",arrayList[indexPath.row][@"out_coming"]];
    
    [cellarray addObject:timest];
    
    [cellarray addObject:typest];
    
    [cellarray addObject:in_com];
    
    [cellarray addObject:out_com];
    
    for (NSInteger i=0; i<4; i++) {
    UILabel *nala=[cell viewWithTag:1000+i];
        
        nala.text=cellarray[i];
        
    }
    
    return cell;
    
}




-(void)backLogin
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
